<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 常用数据操作  -调用时需要进行表名初始化  -function set_table()
 * @author xiahuiqin
 * @version 1.0.0
 */
class MY_Model extends CI_Model  {
    public  $dbname = NULL;
    public  $memcache = NULL;
    public  $redis = NULL;
    public  $table_name = NULL;
	public  function __construct() {
		parent::__construct();
		isset($this->dbname)?$this->load->database($this->dbname):$this->load->database();

        $this->load->helper ( "file" );
	}

	/**
	 * 设置要操作的表名
	 */
	public function set_table($table)
	{
		$this->table_name = $table;
	}
	
	/**
	 * @param Array $where  查询条件
	 * @param Int $limit    限制数据条目
	 * @param Int $offset   偏移量
	 * @param String $order 按照字段排序
	 * @return Array
	 */
	public function get_list($where = '',$limit = '',$offset = '',$order = '',$like = '')
	{
		if($where && is_array($where)){
			foreach ($where as $key => $value)
			{
				$this->db->where($key,$value);
			}
		}
        if($like && is_array($like)){
            foreach ($like as $keyl => $valuel)
            {
                $this->db->like($keyl,$valuel);
            }
        }
		//排序
		if($order)
		{
			$this->db->order_by($order);
		}
		if($limit && is_numeric($limit))
		{
			$this->db->limit($limit);
		}
		//偏移量
		if($offset && is_numeric($offset))
		{
			$this->db->offset($offset);
		}
		return $this->get_result();
	}

    
	/**
	 * 执行SQL
	 * @param String $sql
	 * @return Array
	 */
	public function query($sql)
	{
	   return $this->db->query($sql);
	}
	/**
	 * 插入操作
	 * @param  Array $data
	 * @return int
	 */
	public function insert($data) 
	{
	    $this->db->insert($this->table_name,$data);
	    return  $this->db->affected_rows();
	}
	/**
	 * 插入操作
	 * @param  Array $data
	 * @return int
	 */
	public function insert_id($data) 
	{
	    $this->db->insert($this->table_name,$data);
	    return  $this->db->insert_id();
	}
	
	/**
	 * 更新根据指定列的指定值
	 * @param string $cloum
	 * @param string $value
	 * @param Arrray $data
	 * @return int 
	 */
    public function update_byfield($field,$value,$data)
    {
        $this->db->where($field,$value)->update($this->table_name,$data);
        return $this->db->affected_rows();
    }
    /**
     * 根据条件删除指定数据
     * @param array $condition
     * @return int
     */
    public function del($condition)
    {
    	$this->db->delete($this->table_name,$condition);
    	return  $this->db->affected_rows();
    }
    /**
     * 公用返回结果函数
     * @return array
     */
	private function get_result()
	{
		$result = $this->db->get($this->table_name);
		return $result->result_array();
	}

    /**
     * 返回记录行数
     */
    function get_numrows($where=''){
        if($where && is_array($where)){
            foreach ($where as $key => $value)
            {
                $this->db->where($key,$value);
            }
        }
        $result = $this->db->get($this->table_name);

        return $result->num_rows();
    }
    

    /**
     * 获取单条记录
     * @param int $id
     */
    function get_one_list($id){
    	$this->db->where('id',$id);
    	return $this->db->get($this->table_name)->row_array();
    }
    

}
?>
